<?php
/**
 * PHP Datafeed Library
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.opensource.org/licenses/bsd-license.php
 *
 * @category    Dfp
 * @package     Datafeed
 * @subpackage  File_Reader
 * @copyright   Copyright (c) 2012 PHP Datafeed Library
 * @version     $Id: Reader.php 34 2012-04-13 13:29:03Z t.carnage@gmail.com $
 * @since       2011-12-07
 */
/**
 * Dfp_Datafeed_File_Reader class.
 *
 * @category    Dfp
 * @package     Datafeed
 * @subpackage  File_Reader
 * @copyright   Copyright (c) 2012 PHP Datafeed Library
 * @author      Chris Riley <chris.riley@imhotek.net>
 * @since       2011-12-07
 */
class Dfp_Datafeed_File_Reader implements Dfp_Datafeed_File_Reader_Interface
{
    /**
     * Holds an instance of the feed format adapter.
     *
     * @var Dfp_Datafeed_File_Reader_Format_Interface
     */
    protected $_format;

    /**
     * Holds the name of the format namespace. Defaults to this namespace.
     *
     * @var string
     */
    protected $_formatNamespace = 'Dfp_Datafeed_File_Reader_Format';

	/**
     * @see Dfp_Option_Interface::__construct()
     */
    public function __construct($options = null)
    {
        if ($options instanceof Zend_Config) {
            $this->setConfig($options);
        } elseif (is_array($options)) {
            $this->setOptions($options);
        } elseif (!is_null($options)) {
            throw new Dfp_Datafeed_File_Reader_Exception('Invalid parameter to constructor');
        }
    }

	/**
     * @see Dfp_Datafeed_File_Reader_Interface::getDialect()
     */
    public function getDialect()
    {
        return $this->getFormat()->getDialect();
    }

	/**
     * @see Dfp_Datafeed_File_Reader_Interface::setDialect()
     * @return Dfp_Datafeed_File_Reader
     */
    public function setDialect(Dfp_Datafeed_File_Reader_Format_Dialect_Interface $dialect)
    {
        $this->getFormat()->setDialect($dialect);
        return $this;
    }

	/**
     * @see Dfp_Datafeed_File_Reader_Interface::setDialectString()
     * @return Dfp_Datafeed_File_Reader
     */
    public function setDialectString($dialect)
    {
        $this->getFormat()->setDialectString($dialect);
        return $this;
    }

    /**
     * Returns the format namespace.
     *
     * @return string
     */
    public function getFormatNamespace()
    {
        return $this->_formatNamespace;
    }

    /**
     * Sets the format namespace.
     *
     * @param string $formatNamespace
     */
    public function setFormatNamespace($formatNamespace)
    {
        $this->_formatNamespace = $formatNamespace;
        return $this;
    }

    /**
     * @see Dfp_Datafeed_File_Reader_Interface::getFormat()
     * @return Dfp_Datafeed_File_Reader_Format_Interface
     */
    public function getFormat()
    {
        if (!($this->_format instanceof Dfp_Datafeed_File_Reader_Format_Interface)) {
            if (is_null($this->_format)) {
                $this->_format = 'csv';
            }

            $class = $this->getFormatNamespace() . '_';
            $class .= str_replace(' ', '_', ucwords(str_replace('_', ' ', strtolower($this->_format))));

            $this->_format = new $class();
        }

        return $this->_format;
    }

	/**
     * @see Dfp_Datafeed_File_Reader_Interface::setFormat()
     * @return Dfp_Datafeed_File_Reader
     */
    public function setFormat(Dfp_Datafeed_File_Reader_Format_Interface $format)
    {
        $this->_format = $format;
        return $this;
    }

	/**
     * @see Dfp_Datafeed_File_Reader_Interface::setFormatString()
     * @return Dfp_Datafeed_File_Reader
     */
    public function setFormatString($format)
    {
        $this->_format = $format;
        return $this;
    }

	/**
     * @see Dfp_Datafeed_File_Reader_Interface::getLocation()
     */
    public function getLocation()
    {
        return $this->getFormat()->getLocation();
    }

	/**
     * @see Dfp_Datafeed_File_Reader_Interface::setLocation()
     * @return Dfp_Datafeed_File_Reader
     */
    public function setLocation($location)
    {
        $this->getFormat()->setLocation($location);
        return $this;
    }

    // @codeCoverageIgnoreStart
	/**
     * @see Dfp_Datafeed_File_Reader_Interface::getXslt()
     * @return string
     */
    public function getXslt()
    {
        $format = $this->getFormat();
        if (!($format instanceof Dfp_Datafeed_File_Reader_Format_Xml)) {
            throw new Dfp_Datafeed_File_Reader_Exception('getXslt can only be called when the format is XML');
        }

        return $format->getXslt();
    }

	/**
     * @see Dfp_Datafeed_File_Reader_Interface::setXslt()
     * @return Dfp_Datafeed_File_Reader
     */
    public function setXslt($xslt)
    {
        $format = $this->getFormat();
        if (!($format instanceof Dfp_Datafeed_File_Reader_Format_Xml)) {
            throw new Dfp_Datafeed_File_Reader_Exception('setXslt can only be called when the format is XML');
        }

        $format->setXslt($xslt);
        return $this;
    }
    // @codeCoverageIgnoreEnd
	/**
     * @see Dfp_Option_Interface::setConfig()
     * @return Dfp_Datafeed_File_Reader
     */
    public function setConfig(Zend_Config $config)
    {
        $this->setOptions($config->toArray());
        return $this;
    }

	/**
     * @see Dfp_Option_Interface::setOptions()
     * @return Dfp_Datafeed_File_Reader
     * @throws Dfp_Datafeed_File_Reader_Exception
     */
    public function setOptions(array $options)
    {
        if (isset($options['format'])) {
            if ($options['format'] instanceof Dfp_Datafeed_File_Reader_Format_Interface) {
                $this->setFormat($options['format']);
            } elseif (is_string($options['format'])) {
                $this->setFormatString($options['format']);
            } else {
                throw new Dfp_Datafeed_File_Reader_Exception('Invalid format specified');
            }
            unset($options['format']);
        }

        $this->getFormat()->setOptions($options);
        return $this;
    }

	/**
     * @see Iterator::current()
     */
    public function current()
    {
        return $this->getFormat()->current();
    }

	/**
     * @see Iterator::key()
     */
    public function key()
    {
        return $this->getFormat()->key();
    }

	/**
     * @see Iterator::next()
     */
    public function next()
    {
        $this->getFormat()->next();
    }

	/**
     * @see Iterator::rewind()
     */
    public function rewind()
    {
        $this->getFormat()->rewind();
    }

	/**
     * @see Iterator::valid()
     */
    public function valid()
    {
        return $this->getFormat()->valid();
    }

	/**
     * @see Dfp_Error_Interface::addError()
     * @return Dfp_Datafeed_File_Reader
     */
    public function addError($message)
    {
        $this->getFormat()->addError($message);
        return $this;
    }

	/**
     * @see Dfp_Error_Interface::addErrors()
     * @return Dfp_Datafeed_File_Reader
     */
    public function addErrors(array $messages)
    {
        $this->getFormat()->addErrors($messages);
        return $this;
    }

	/**
     * @see Dfp_Error_Interface::getErrors()
     * @return array
     */
    public function getErrors()
    {
        return $this->getFormat()->getErrors();
    }

	/**
     * @see Dfp_Error_Interface::hasErrors()
     * @return bool
     */
    public function hasErrors()
    {
        return $this->getFormat()->hasErrors();
    }

	/**
     * @see Dfp_Error_Interface::setErrors()
     * @return Dfp_Datafeed_File_Reader
     */
    public function setErrors(array $messages)
    {
        $this->getFormat()->setErrors($messages);
        return $this;
    }
}